package com.ebupt.migu.music.system.service;

import com.ebupt.migu.music.common.exception.MyException;
import com.ebupt.migu.music.common.exception.StatusEnum;
import com.ebupt.migu.music.system.entity.MenuBuilder;
import com.ebupt.migu.music.system.entity.SysMenu;
import com.ebupt.migu.music.system.mapper.MenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class MenuService {
	@Autowired
	private MenuMapper menuMapper;

	/**
	 * 添加菜单
	 * @param paramMap
	 * @return
	 */
	public String addMenu(Map<String,Object> paramMap){
		try {
			menuMapper.addMenu(paramMap);
			return paramMap.get("id").toString();
		} catch (Exception e) {
			throw new MyException(StatusEnum.ERROR, e.getMessage());
		}
	}
	
	/**
	 * 删除菜单
	 * @param paramMap
	 * @return
	 */
	public void delMenu(Map<String,Object> paramMap){
		try {
			menuMapper.delMenu(paramMap);
		} catch (Exception e) {
			throw new MyException(StatusEnum.ERROR, e.getMessage());
		}
	}

	/**
	 * 获取菜单列表（树状结构）
	 * @param paramMap
	 * @return
	 */
	public List<SysMenu> getMenuList(Map<String,Object> paramMap){
		try {
			List<SysMenu> list = menuMapper.getMenuList(paramMap);
			return MenuBuilder.buildByRecursive(list);
		} catch (Exception e) {
			throw new MyException(StatusEnum.ERROR, e.getMessage());
		}
	}

	/**
	 * 添加角色
	 * @param paramMap
	 * @return
	 */
	public String modifyMenu(Map<String,Object> paramMap){
		try {
			//更新菜单信息
			menuMapper.modifyMenu(paramMap);
			return paramMap.get("menuId").toString();
		} catch (Exception e) {
			throw new MyException(StatusEnum.ERROR, e.getMessage());
		}
	}

	/**
	 * 根据用户id，构建菜单树
	 * @param userId 用户id
	 * @return
	 */
	public List<SysMenu> getMenuByUserId(Integer userId){
		return MenuBuilder.buildByRecursiveForLogin(menuMapper.getMenuByUserId(userId.toString()));
	}

}
